/** 
 * Project Name: antiques 
 * File Name: StringUtil.java 
 * Package Name: com.sinosoft.antiques.util 
 * Date: 2016年11月29日下午2:37:47 
 * Copyright (c) 2016, ISCAS Rights Reserved. 
 */

package com.sinosoft.antiques.util;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sinosoft.app.service.reverse.push.DateTimeUtil;
import com.sinosoft.commontools.str.StrHelp;

/**
 * ClassName: StringUtil <br/>
 * Date: 2016年11月29日 下午2:37:47 <br/>
 * 
 * @author paddy
 * @version 1.0
 * @since JDK 1.7
 * @see
 */
public class StringUtil {
	/**
	 * @Description: 根据当前时间生成一个20位长度的id
	 * 
	 * @author paddy
	 * @since JDK 1.7
	 * @Date 2016年11月29日 下午2:42:02
	 * @return
	 */
	public static String genId() {
		String random = String.valueOf(new Random().nextInt(999));
		// 补齐3位
		if (random.length() == 2)
			random = "0" + random;
		else if (random.length() == 1)
			random = "00" + random;

		// 精确到毫秒
		return DateTimeUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS")
				+ random;
	}

	public static int getLen(String str) {
		int res = 0;
		if (StrHelp.isValidStr(str)) {
			for (int i = 0; i < str.length(); i++) {
				String c = str.substring(i, i + 1);

				String regEx = "^[A-Za-z0-9_.-]+$";
				// 编译正则表达式
				Pattern pattern = Pattern.compile(regEx);
				// 忽略大小写的写法
				// Pattern pat = Pattern.compile(regEx,
				// Pattern.CASE_INSENSITIVE);
				Matcher matcher = pattern.matcher(c);
				// 字符串是否与正则表达式相匹配
				boolean rs = matcher.matches();
				if (rs)
					res++;
				else {
					res += 2;
				}
			}
		}
		return res;
	}

	/**
	 * isEmpty(是否为空判断)
	 * 
	 * @author wangli
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || str.length() <= 0) {
			return true;
		}
		return false;
	}

	/**
	 * isNoEmpty(是否不为空判断)
	 * 
	 * @author wangli
	 * @param str
	 * @return
	 */
	public static boolean isNoEmpty(String str) {
		if (str == null || str.length() <= 0) {
			return false;
		}
		return true;
	}

	/**
	 * replace(替换字符串)
	 * 
	 * @author wangli
	 * @param src
	 *            源字符串
	 * @param oldstr
	 *            要替换的字符串
	 * @param newstr
	 *            新字符串
	 * @return
	 */
	public static String replace(String src, String oldstr, String newstr) {
		StringBuffer dest = new StringBuffer();
		int beginIndex = 0;
		int endIndex = 0;
		while (true) {
			endIndex = src.indexOf(oldstr, beginIndex);
			if (endIndex >= 0) {
				dest.append(src.substring(beginIndex, endIndex));
				dest.append(newstr);
				beginIndex = endIndex + oldstr.length();
			} else {
				dest.append(src.substring(beginIndex));
				break;
			}
		}
		return dest.toString();
	}

	public static boolean isNullOrEmpty(Object obj) {
		try {
			if (obj == null)
				return true;
			if (obj instanceof CharSequence) {
				return ((CharSequence) obj).length() == 0;
			}
			if (obj instanceof Collection) {
				return ((Collection<?>) obj).isEmpty();
			}
			if (obj instanceof Map) {
				return ((Map<?, ?>) obj).isEmpty();
			}
			if (obj instanceof Object[]) {
				Object[] object = (Object[]) obj;
				if (object.length == 0) {
					return true;
				}
				boolean empty = true;
				for (int i = 0; i < object.length; i++) {
					if (!isNullOrEmpty(object[i])) {
						empty = false;
						break;
					}
				}
				return empty;
			}
			return false;
		} catch (Exception e) {
			return true;
		}

	}

	/**
	 * sort 顺排序
	 * 
	 * @author wangli
	 * @param ss
	 * @return
	 */
	public static int[] sort(int[] ss) {
		if (ss != null && ss.length > 0) {
			for (int i = 1; i < ss.length; i++) {
				int key = ss[i];
				int j = i - 1;
				while (j > -1 && ss[j] > key) {
					ss[j + 1] = ss[j];
					j -= 1;
				}
				ss[j + 1] = key;
			}
		}
		return ss;
	}

	/**
	 * sortdesc 逆排序
	 * 
	 * @author wangli
	 * @param ss
	 * @return
	 */
	public static int[] sortdesc(int[] ss) {
		if (ss != null && ss.length > 0) {
			for (int i = 0; i < ss.length; i++) {
				int key = ss[i];
				int j = i - 1;
				while (j > -1 && ss[j] < key) {
					ss[j + 1] = ss[j];
					j -= 1;
				}
				ss[j + 1] = key;
			}
		}
		return ss;
	}

	/**
	 * 生成字符串
	 * 
	 * @return
	 */
	public static String getUUID24() {
		UUID id = UUID.randomUUID();
		String[] idd = id.toString().split("-");
		return idd[0] + idd[1] + idd[4];
	}

	public static void main(String[] args) {
		String str = "";
		String str2 = null;
		System.out.println(isNullOrEmpty(str));
		System.out.println(isNullOrEmpty(str2));
		System.out.println(isNullOrEmpty(0));

	}

}
